//通用库函数
#include<string.h>
#include<stdio.h>
//内部头文件
#include "WifiConfig_hal_sys_api.h"
//FreeRTOS
#include <FreeRTOS.h>
#include <task.h>

#include <wifi_mgmr_ext.h>
#include <hal_wifi.h>

uint8_t wifiStatus;

static void _hal_thread_func_wrapper_(void *ptr);

int AI_ComboHalWifiConfigBaseInit(void){
	//空函数，需要调用这个函数
	//bl的SDK需要调用一下，否则当前文件中的函数不会被编译(函数会执行对应的弱定义)
	return 0;
}

//----------------------------------需要适配的函数------------------------
//根据SSID和密码连接WIFI
//成功 0 失败 AT_ERR_FAILURE

char ak_wifi_connetct(const char *ssid, const char *pwd){
	printf("ak_wifi_connetct:ssid=%s pwd=%s\r\n",ssid,pwd);
    wifi_interface_t wifi_interface;

    wifi_interface = wifi_mgmr_sta_enable();
    uint8_t u8_ret=wifi_mgmr_sta_connect(wifi_interface, ssid, pwd, NULL, NULL, 0, 0);
	if(u8_ret!=0)
	{
		printf("[ak_wifi_connetct]AI_HalConnectToAP error,ret=%d\r\n",u8_ret);
		return AT_ERR_FAILURE;
	}
	else
	{
		wifiStatus=AI_CONNECT_STATUS_CONNECTED;
		return 0;
	}

}

//打印获取到的wifi配网信息
void axk_at_wifi_sc_get_info_export(const char *ssid, const char *pwd){
	printf("Smart get wifi info\r\nssid:%s\r\npassword:%s\r\n",ssid,pwd);
}

//获取当前wifi的状态
at_wifi_state_t WifiConfigGetWifiStatus(){
	switch(wifiStatus){
		case AI_CONNECT_STATUS_CONNECTING:
			return AT_WIFI_STATE_CONNECTING;
		case AI_WIFI_STATUS_CONNECT_NO_IP:
			return AT_WIFI_STATE_CONNECTED_NOT_GET_IP;
		case AI_CONNECT_STATUS_CONNECTED:
			return AT_WIFI_STATE_CONNECTED_GOT_IP;    
	    case AI_CONNECT_STATUS_DISCONNECT:	//没有连接，或者已经断开
	    	return AT_WIFI_STATE_DISCONNECTED;
		default:
			return AT_WIFI_STATE_NOT_STARTUP;
	}

	return 0;
}

//创建线程
int axk_hal_thread_create(at_hal_threadParams_t *params){
    if (params == NULL)
        return -1;

    if (params->thread_name == NULL) {
        printf("[OS] thread_name is NULL \r\n");
        return -1;
    }

    int ret = xTaskCreate(_hal_thread_func_wrapper_, params->thread_name, (params->stack_size), (void *)params,
                          params->priority, (void *)&params->thread_id);
    if (ret != pdPASS) {
        printf("[OS] xTaskCreate failed: %d\n", ret);
        return -1;
    }

    return 0;
}

//wifi 连接成功回调函数，这里可以添加保存上电自动连接代码
void axk_at_wifi_sc_connect_ap_success_export(const char *ssid, const char *pwd){
    printf("Smartconfig connected Wi-Fi\r\n");
}


 extern wifi_evt_cb wificonfigTmpCallBack;
//wifi事件 WAIT_TODO 改成回调事件
void wifi_event_register(wifi_evt_cb cb){
    wificonfigTmpCallBack = cb;
}

int axk_hal_thread_destroy(at_hal_threadParams_t *params){
    if(params->thread_id == NULL)
    {
        return -1;
    }
    vTaskDelete((TaskHandle_t)params->thread_id);
    return 0;
}

int wifi_channel_to_freq(int channel)
{
    int freq = 0;

    do
    {
        // Check if the channel number is in the expected range
        if ((channel < 1) || (channel > 14))
            break;

        // Compute the channel number
        if (channel == 14)
            freq = 2484;
        else
            freq = 2407 + channel * 5;
    } while(0);

    return freq;
}


void wifi_sta_connect_sc(const char *ssid, const char *pwd, int channel){
	ak_wifi_connetct(ssid,pwd);
}

//配网启动的时候会设置一下(还没配网成功的时候)
int axk_hal_wifi_mode_set(uint8_t mode, uint8_t auto_conn){
	printf("[%s()-%d]mode=%d auto_conn=%d\r\n",__func__,__LINE__,mode,auto_conn);
	return 0;
}

//------------------------------------------------------------- 内部调用 ------------------------------------------
static void _hal_thread_func_wrapper_(void *ptr)
{
    at_hal_threadParams_t *params = (at_hal_threadParams_t *)ptr;

    params->thread_func(params->user_arg);

    params->thread_id = NULL;

    vTaskDelete(NULL);
}


